import {JsonController,Controller, Render, Get, UseBefore, Session,Req,Res,Ctx,QueryParams,UseInterceptor,Post,Body} from "routing-controllers";
import { config } from "../../config";
import { utils } from "../../utils/utils";
import  rsa  from "../../utils/rsa";
import { Service, Inject } from "typedi";
import {UserServiceImpl} from '../../service/impl/UserServiceImpl'
import TokenLoginMiddlewares from '../../middleware/TokenLoginMiddlewares';
import { JsonResponseInterceptor } from "../../middleware/JsonResponseMiddlewares";
import {PhoneLoginModel,ChangeUserFollowModel,getUserData,getUserDynamicData,getUserUserListData,getUserLikeListModel,updateUserDataModel} from '../../model/UserModel'

@Service()
@JsonController(`${config.apiurl}/v1/user`)
@UseInterceptor(JsonResponseInterceptor)
export class UserControllerApi {

  static _userType:string|object;


    @Inject()
    private userServiceImpl!:UserServiceImpl;


    @Post("/userLogin")
    public async userLogin (@Res() response: any,@Session() session:any, @Body() params:PhoneLoginModel): Promise<any> {

      
      params.user_Phone = await rsa.decryptPrivate(params.user_Phone);
      params.user_PassWord =await rsa.decryptPrivate(params.user_PassWord);

      UserControllerApi._userType =   await this.userServiceImpl.isUser(params)

        if(typeof UserControllerApi._userType == 'string'){
          response.message = UserControllerApi._userType;
          return;
        }else{
        return  await rsa.encryptPublic(JSON.stringify(UserControllerApi._userType));
        }
        
    }

    @Get("/changeUserFollow")
    @UseBefore(TokenLoginMiddlewares)
    public async changeUserFollow (@Res() response: any,@Session() session:any,@QueryParams() params:ChangeUserFollowModel): Promise<any> {
        return await this.userServiceImpl.changeUserFollow(params);
    }
    
    
    @Get("/getUserData")
    @UseBefore(TokenLoginMiddlewares)
    public async getUserData (@Res() response: any,@Session() session:any,@QueryParams() params:getUserData): Promise<any> {
        return await this.userServiceImpl.getUserData(params);
    }

    @Get("/getUserDynamic")
    @UseBefore(TokenLoginMiddlewares)
    public async getUserDynamic (@Res() response: any,@Session() session:any,@QueryParams() params:getUserDynamicData): Promise<any> {
        return {dynamic_List:await this.userServiceImpl.getUserDynamic(params)};
    }

    @Get("/getUserFollowOrFans")
    @UseBefore(TokenLoginMiddlewares)
    public async getUserFollowOrFans (@Res() response: any,@Session() session:any,@QueryParams() params:getUserUserListData): Promise<any> {

        return {user_List:await this.userServiceImpl.getUserFollowOrFans(params)};
    }

    @Get("/getPersonalThinkList")
    @UseBefore(TokenLoginMiddlewares)
    public async getLikeThinkList (@Res() response: any,@Session() session:any,@QueryParams() params:getUserLikeListModel): Promise<any> {
      return {thinkData:await this.userServiceImpl.getPersonalThinkList(params)};
    }

    @Get("/getPersonalDynamicList")
    @UseBefore(TokenLoginMiddlewares)
    public async getLikeDynamicList (@Res() response: any,@Session() session:any,@QueryParams() params:getUserLikeListModel): Promise<any> {
        return {dynamic_List:await this.userServiceImpl.getPersonalDynamicList(params)};
    }

    @Get("/updateUserData")
    @UseBefore(TokenLoginMiddlewares)
    public async updateUserData (@Res() response: any,@Session() session:any,@QueryParams() params:updateUserDataModel): Promise<any> {
      params.user_NickName = utils.utf16toEntities(params.user_NickName);
        return await this.userServiceImpl.updateUserData(params);
    }

   
  }

